
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>标准流与文件 · Dart编程指南</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="arcticfox">
        
        
    
    <link rel="stylesheet" href="gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-anchor-navigation-ex/style/plugin.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-search-pro/search.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-pageview-count/plugin.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-theme-comscore/test.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="运算符.html" />
    
    
    <link rel="prev" href="生成器.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    
    
        
        <li>
            <a href="http://www.bczl.xyz/" target="_blank" class="custom-link">主页</a>
        </li>
    
    

    
    <li class="divider"></li>
    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="./">
            
                <a href="./">
            
                    
                    前言
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="基础篇.html">
            
                <a href="基础篇.html">
            
                    
                    基础篇
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="环境搭建.html">
            
                <a href="环境搭建.html">
            
                    
                    环境搭建
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="简单语法.html">
            
                <a href="简单语法.html">
            
                    
                    简单语法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="函数.html">
            
                <a href="函数.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="进阶篇.html">
            
                <a href="进阶篇.html">
            
                    
                    进阶篇
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="面向对象.html">
            
                <a href="面向对象.html">
            
                    
                    面向对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="泛型.html">
            
                <a href="泛型.html">
            
                    
                    泛型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="枚举.html">
            
                <a href="枚举.html">
            
                    
                    枚举
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="库与导入.html">
            
                <a href="库与导入.html">
            
                    
                    库与导入
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="异步编程.html">
            
                <a href="异步编程.html">
            
                    
                    异步编程
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.5.1" data-path="异步编程之Future.html">
            
                <a href="异步编程之Future.html">
            
                    
                    Future
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5.2" data-path="异步编程之Stream.html">
            
                <a href="异步编程之Stream.html">
            
                    
                    Stream
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5.3" data-path="异步编程之Isolate.html">
            
                <a href="异步编程之Isolate.html">
            
                    
                    Isolate
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="异常处理.html">
            
                <a href="异常处理.html">
            
                    
                    异常处理与 Zone
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.7" data-path="生成器.html">
            
                <a href="生成器.html">
            
                    
                    生成器
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.3.8" data-path="标准流与文件.html">
            
                <a href="标准流与文件.html">
            
                    
                    标准流与文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.9" data-path="运算符.html">
            
                <a href="运算符.html">
            
                    
                    附：运算符
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="常用标准库.html">
            
                <a href="常用标准库.html">
            
                    
                    常用标准库
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="字符串.html">
            
                <a href="字符串.html">
            
                    
                    字符串
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="集合.html">
            
                <a href="集合.html">
            
                    
                    集合
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="URI处理.html">
            
                <a href="URI处理.html">
            
                    
                    URI处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="日期和时间.html">
            
                <a href="日期和时间.html">
            
                    
                    日期和时间
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="环境及外部调用.html">
            
                <a href="环境及外部调用.html">
            
                    
                    环境及外部调用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="其他.html">
            
                <a href="其他.html">
            
                    
                    其他
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="实战篇.html">
            
                <a href="实战篇.html">
            
                    
                    实战篇
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="网络编程.html">
            
                <a href="网络编程.html">
            
                    
                    网络编程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="爬虫实战.html">
            
                <a href="爬虫实战.html">
            
                    
                    爬虫实战
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="HTTP服务器.html">
            
                <a href="HTTP服务器.html">
            
                    
                    HTTP服务器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="WebSocket服务器.html">
            
                <a href="WebSocket服务器.html">
            
                    
                    WebSocket服务器
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" >
            
                <span>
            
                    
                    附录
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="dart2.14.html">
            
                <a href="dart2.14.html">
            
                    
                    升级到Dart 2.14
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="http://www.bczl.xyz/" target="blank" class="gitbook-link">
            编程之路
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >标准流与文件</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <div id="anchor-navigation-ex-navbar"><i class="fa fa-navicon"></i><ul><ul><li><span class="title-icon "></span><a href="#&#x6807;&#x51C6;&#x8F93;&#x5165;&#x8F93;&#x51FA;&#x6D41;"><b></b>&#x6807;&#x51C6;&#x8F93;&#x5165;&#x8F93;&#x51FA;&#x6D41;</a></li><li><span class="title-icon "></span><a href="#&#x6587;&#x4EF6;&#x64CD;&#x4F5C;"><b></b>&#x6587;&#x4EF6;&#x64CD;&#x4F5C;</a></li><ul><li><span class="title-icon "></span><a href="#&#x5199;&#x6587;&#x4EF6;"><b></b>&#x5199;&#x6587;&#x4EF6;</a></li><li><span class="title-icon "></span><a href="#&#x8BFB;&#x6587;&#x4EF6;"><b></b>&#x8BFB;&#x6587;&#x4EF6;</a></li><li><span class="title-icon "></span><a href="#&#x6587;&#x4EF6;&#x7684;&#x5176;&#x4ED6;&#x64CD;&#x4F5C;"><b></b>&#x6587;&#x4EF6;&#x7684;&#x5176;&#x4ED6;&#x64CD;&#x4F5C;</a></li></ul></ul></ul></div><a href="#" id="anchorNavigationExGoTop"><i class="fa fa-arrow-up"></i></a><!-- ex_nolevel -->
<h2 id="&#x6807;&#x51C6;&#x8F93;&#x5165;&#x8F93;&#x51FA;&#x6D41;"><a name="&#x6807;&#x51C6;&#x8F93;&#x5165;&#x8F93;&#x51FA;&#x6D41;" class="anchor-navigation-ex-anchor" href="#&#x6807;&#x51C6;&#x8F93;&#x5165;&#x8F93;&#x51FA;&#x6D41;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x6807;&#x51C6;&#x8F93;&#x5165;&#x8F93;&#x51FA;&#x6D41;</h2>
<ul>
<li><code>stdin</code></li>
<li><code>stdout</code></li>
<li><code>stderr</code></li>
</ul>
<pre><code class="lang-dart"><span class="hljs-comment">// &#x5BFC;&#x5165;io&#x5305;</span>
<span class="hljs-keyword">import</span> <span class="hljs-string">&apos;dart:io&apos;</span>;

<span class="hljs-keyword">void</span> main() {
  <span class="hljs-comment">// &#x5411;&#x6807;&#x51C6;&#x8F93;&#x51FA;&#x6D41;&#x5199;&#x5B57;&#x7B26;&#x4E32;</span>
  stdout.write(<span class="hljs-string">&apos;root\$:&apos;</span>);
  <span class="hljs-comment">// &#x4ECE;&#x6807;&#x51C6;&#x8F93;&#x5165;&#x6D41;&#x8BFB;&#x53D6;&#x4E00;&#x884C;&#x5B57;&#x7B26;&#x4E32;</span>
  <span class="hljs-keyword">var</span> input = stdin.readLineSync();
  <span class="hljs-comment">// &#x5E26;&#x6362;&#x884C;&#x7B26;&#x7684;&#x5199;&#x6570;&#x636E;</span>
  stdout.writeln(<span class="hljs-string">&quot;input data:$input&quot;</span>);
  <span class="hljs-comment">// &#x5411;&#x6807;&#x51C6;&#x9519;&#x8BEF;&#x6D41;&#x5199;&#x6570;&#x636E;</span>
  stderr.writeln(<span class="hljs-string">&quot;has not error&quot;</span>);
}
</code></pre>
<p><code>stdin</code>&#x9664;&#x4E86;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;<code>readLineSync</code>&#x8BFB;&#x4E00;&#x884C;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x8FD8;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;<code>readByteSync</code>&#x8BFB;&#x53D6;&#x4E00;&#x4E2A;&#x5B57;&#x8282;&#x3002;</p>
<h2 id="&#x6587;&#x4EF6;&#x64CD;&#x4F5C;"><a name="&#x6587;&#x4EF6;&#x64CD;&#x4F5C;" class="anchor-navigation-ex-anchor" href="#&#x6587;&#x4EF6;&#x64CD;&#x4F5C;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x6587;&#x4EF6;&#x64CD;&#x4F5C;</h2>
<h3 id="&#x5199;&#x6587;&#x4EF6;"><a name="&#x5199;&#x6587;&#x4EF6;" class="anchor-navigation-ex-anchor" href="#&#x5199;&#x6587;&#x4EF6;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x5199;&#x6587;&#x4EF6;</h3>
<p>&#x4E00;&#x79CD;&#x7B80;&#x4FBF;&#x7684;&#x64CD;&#x4F5C;&#x65B9;&#x5F0F;&#xFF0C;&#x65E0;&#x9700;&#x624B;&#x52A8;&#x5173;&#x95ED;&#x6587;&#x4EF6;&#xFF0C;&#x6587;&#x4EF6;&#x5199;&#x5165;&#x5B8C;&#x6210;&#x540E;&#x4F1A;&#x81EA;&#x52A8;&#x5173;&#x95ED;</p>
<pre><code class="lang-dart"><span class="hljs-keyword">import</span> <span class="hljs-string">&apos;dart:io&apos;</span>;

<span class="hljs-keyword">void</span> main() <span class="hljs-keyword">async</span>{
  <span class="hljs-comment">// &#x521B;&#x5EFA;&#x6587;&#x4EF6;</span>
  File file = <span class="hljs-keyword">new</span> File(<span class="hljs-string">&apos;test.txt&apos;</span>);
  <span class="hljs-built_in">String</span> content = <span class="hljs-string">&apos;The easiest way to write text to a file is to create a File&apos;</span>;

  <span class="hljs-keyword">try</span> {
    <span class="hljs-comment">// &#x5411;&#x6587;&#x4EF6;&#x5199;&#x5165;&#x5B57;&#x7B26;&#x4E32;</span>
    <span class="hljs-keyword">await</span> file.writeAsString(content);
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&apos;Data written.&apos;</span>);
  } <span class="hljs-keyword">catch</span> (e) {
    <span class="hljs-built_in">print</span>(e);
  }
}
</code></pre>
<p><code>writeAsString</code>&#x539F;&#x578B;</p>
<pre><code class="lang-dart">  Future&lt;File&gt; writeAsString(<span class="hljs-built_in">String</span> contents,
      {FileMode mode: FileMode.write,
      Encoding encoding: utf8,
      <span class="hljs-built_in">bool</span> flush: <span class="hljs-keyword">false</span>})
</code></pre>
<ul>
<li><code>mode</code> &#x6587;&#x4EF6;&#x6A21;&#x5F0F;&#xFF0C;&#x8FD9;&#x91CC;&#x9ED8;&#x8BA4;&#x4E3A;&#x5199;&#x6A21;&#x5F0F;</li>
<li><code>encoding</code> &#x5B57;&#x7B26;&#x7F16;&#x7801;&#xFF0C;&#x9ED8;&#x8BA4;&#x4E3A;utf-8</li>
<li><code>flush</code> &#x662F;&#x5426;&#x7ACB;&#x523B;&#x5237;&#x65B0;&#x7F13;&#x5B58;&#xFF0C;&#x9ED8;&#x8BA4;&#x4E3A;false</li>
</ul>
<p>&#x6587;&#x4EF6;&#x6A21;&#x5F0F;<code>FileMode</code>&#x7684;&#x5E38;&#x91CF;</p>
<table>
<thead>
<tr>
<th style="text-align:left">&#x5E38;&#x91CF;&#x503C;</th>
<th style="text-align:left">&#x8BF4;&#x660E;</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">read</td>
<td style="text-align:left">&#x53EA;&#x8BFB;&#x6A21;&#x5F0F;</td>
</tr>
<tr>
<td style="text-align:left">write</td>
<td style="text-align:left">&#x53EF;&#x8BFB;&#x53EF;&#x5199;&#x6A21;&#x5F0F;&#xFF0C;&#x5982;&#x679C;&#x6587;&#x4EF6;&#x5B58;&#x5728;&#x5219;&#x4F1A;&#x8986;&#x76D6;</td>
</tr>
<tr>
<td style="text-align:left">append</td>
<td style="text-align:left">&#x8FFD;&#x52A0;&#x6A21;&#x5F0F;&#xFF0C;&#x53EF;&#x8BFB;&#x53EF;&#x5199;&#xFF0C;&#x6587;&#x4EF6;&#x5B58;&#x5728;&#x5219;&#x5F80;&#x672B;&#x5C3E;&#x8FFD;&#x52A0;</td>
</tr>
<tr>
<td style="text-align:left">writeOnly</td>
<td style="text-align:left">&#x53EA;&#x5199;&#x6A21;&#x5F0F;</td>
</tr>
<tr>
<td style="text-align:left">writeOnlyAppend</td>
<td style="text-align:left">&#x53EA;&#x5199;&#x6A21;&#x5F0F;&#x4E0B;&#x7684;&#x8FFD;&#x52A0;&#x6A21;&#x5F0F;&#xFF0C;&#x4E0D;&#x53EF;&#x8BFB;</td>
</tr>
</tbody>
</table>
<p>&#x9664;&#x4E86;<code>writeAsString</code>&#x65B9;&#x6CD5;&#x5916;&#xFF0C;&#x8FD8;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;<code>writeAsBytes</code>&#x5199;&#x5165;&#x4E00;&#x4E2A;&#x5B57;&#x8282;&#x5217;&#x8868;&#x3002;&#x9700;&#x8981;&#x6CE8;&#x610F;&#x7684;&#x662F;&#xFF0C;&#x8FD9;&#x4E24;&#x4E2A;&#x65B9;&#x6CD5;&#x90FD;&#x662F;&#x5F02;&#x6B65;&#x6267;&#x884C;&#x7684;&#xFF0C;&#x8FD4;&#x56DE;&#x503C;&#x90FD;&#x662F;<code>Future</code>&#xFF0C;&#x5982;&#x679C;&#x6709;&#x5FC5;&#x8981;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x540C;&#x6B65;&#x65B9;&#x6CD5;&#x6267;&#x884C;&#x5199;&#x5165;&#x64CD;&#x4F5C;</p>
<ul>
<li><code>writeAsStringSync</code></li>
<li><code>writeAsBytesSync</code></li>
</ul>
<p>&#x5982;&#x9700;&#x8981;&#x66F4;&#x7075;&#x6D3B;&#x7684;&#x63A7;&#x5236;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x5982;&#x4E0B;&#x65B9;&#x5F0F;&#x64CD;&#x4F5C;&#x6587;&#x4EF6;&#xFF0C;&#x4F46;&#x662F;&#x9700;&#x8981;&#x624B;&#x52A8;&#x5173;&#x95ED;&#x6587;&#x4EF6;</p>
<pre><code class="lang-dart"><span class="hljs-keyword">import</span> <span class="hljs-string">&apos;dart:io&apos;</span>;

<span class="hljs-keyword">void</span> main() <span class="hljs-keyword">async</span>{
  <span class="hljs-comment">// &#x521B;&#x5EFA;&#x6587;&#x4EF6;</span>
  File file = <span class="hljs-keyword">new</span> File(<span class="hljs-string">&apos;test.txt&apos;</span>);
  <span class="hljs-comment">// &#x6587;&#x4EF6;&#x6A21;&#x5F0F;&#x8BBE;&#x7F6E;&#x4E3A;&#x8FFD;&#x52A0;</span>
  IOSink isk = file.openWrite(mode: FileMode.append);

  <span class="hljs-comment">// &#x591A;&#x6B21;&#x5199;&#x5165;</span>
  isk.write(<span class="hljs-string">&apos;A woman is like a tea bag&apos;</span>);
  isk.writeln(<span class="hljs-string">&apos;you never know how strong it is until it\&apos;s in hot water.&apos;</span>);
  isk.writeln(<span class="hljs-string">&apos;-Eleanor Roosevelt&apos;</span>);
  <span class="hljs-keyword">await</span> isk.close();
  <span class="hljs-built_in">print</span>(<span class="hljs-string">&apos;Done!&apos;</span>);
}
</code></pre>
<h3 id="&#x8BFB;&#x6587;&#x4EF6;"><a name="&#x8BFB;&#x6587;&#x4EF6;" class="anchor-navigation-ex-anchor" href="#&#x8BFB;&#x6587;&#x4EF6;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x8BFB;&#x6587;&#x4EF6;</h3>
<p>&#x7B80;&#x4FBF;&#x7684;&#x65B9;&#x5F0F;</p>
<ul>
<li><code>readAsBytes</code></li>
<li><code>readAsBytesSync</code></li>
<li><code>readAsString</code></li>
<li><code>readAsStringSync</code></li>
<li><code>readAsLines</code></li>
<li><code>readAsLinesSync</code></li>
</ul>
<pre><code class="lang-dart"><span class="hljs-keyword">void</span> main() <span class="hljs-keyword">async</span>{
  File file = <span class="hljs-keyword">new</span> File(<span class="hljs-string">&apos;test.txt&apos;</span>);
  <span class="hljs-keyword">try</span>{
    <span class="hljs-built_in">String</span> content = <span class="hljs-keyword">await</span> file.readAsString();
    <span class="hljs-built_in">print</span>(content);
  }<span class="hljs-keyword">catch</span>(e){
    <span class="hljs-built_in">print</span>(e);
  }
}
</code></pre>
<p>&#x53E6;&#x4E00;&#x79CD;&#x66F4;&#x4F4E;&#x7EA7;&#x522B;&#x7684;&#x65B9;&#x5F0F;</p>
<pre><code class="lang-dart"><span class="hljs-keyword">import</span> <span class="hljs-string">&apos;dart:io&apos;</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">&apos;dart:convert&apos;</span>;

<span class="hljs-keyword">void</span> main() <span class="hljs-keyword">async</span>{
  <span class="hljs-keyword">try</span> {
    <span class="hljs-comment">// LineSplitter Dart&#x8BED;&#x8A00;&#x5C01;&#x88C5;&#x7684;&#x6362;&#x884C;&#x7B26;&#xFF0C;&#x6B64;&#x5904;&#x5C06;&#x6587;&#x672C;&#x6309;&#x884C;&#x5206;&#x5272;</span>
    Stream lines = <span class="hljs-keyword">new</span> File(<span class="hljs-string">&apos;test.txt&apos;</span>).openRead()
        .transform(utf8.decoder).transform(<span class="hljs-keyword">const</span> LineSplitter());

    <span class="hljs-keyword">await</span> <span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> line <span class="hljs-keyword">in</span> lines) {
      <span class="hljs-built_in">print</span>(line);
    }
  } <span class="hljs-keyword">catch</span> (_) {}
}
</code></pre>
<h3 id="&#x6587;&#x4EF6;&#x7684;&#x5176;&#x4ED6;&#x64CD;&#x4F5C;"><a name="&#x6587;&#x4EF6;&#x7684;&#x5176;&#x4ED6;&#x64CD;&#x4F5C;" class="anchor-navigation-ex-anchor" href="#&#x6587;&#x4EF6;&#x7684;&#x5176;&#x4ED6;&#x64CD;&#x4F5C;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x6587;&#x4EF6;&#x7684;&#x5176;&#x4ED6;&#x64CD;&#x4F5C;</h3>
<pre><code class="lang-dart"><span class="hljs-keyword">import</span> <span class="hljs-string">&apos;dart:io&apos;</span>;

<span class="hljs-keyword">void</span> main() <span class="hljs-keyword">async</span>{
  File file = <span class="hljs-keyword">new</span> File(<span class="hljs-string">&apos;test.txt&apos;</span>);

  <span class="hljs-comment">// &#x5224;&#x65AD;&#x6587;&#x4EF6;&#x662F;&#x5426;&#x5B58;&#x5728;</span>
  <span class="hljs-keyword">if</span>(<span class="hljs-keyword">await</span> file.exists()){
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;&#x6587;&#x4EF6;&#x5B58;&#x5728;&quot;</span>);
  }<span class="hljs-keyword">else</span>{
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;&#x6587;&#x4EF6;&#x4E0D;&#x5B58;&#x5728;&quot;</span>);
  }

  <span class="hljs-comment">// &#x590D;&#x5236;&#x6587;&#x4EF6;</span>
  <span class="hljs-keyword">await</span> file.copy(<span class="hljs-string">&quot;test-1.txt&quot;</span>);

  <span class="hljs-comment">// &#x4FEE;&#x6539;&#x6587;&#x4EF6;&#x540D;&#x3002;&#x5F53;&#x4F20;&#x5165;&#x4E0D;&#x540C;&#x8DEF;&#x5F84;&#x65F6;&#xFF0C;&#x53EF;&#x7528;&#x6765;&#x79FB;&#x52A8;&#x6587;&#x4EF6;</span>
  <span class="hljs-keyword">await</span> file.rename(<span class="hljs-string">&quot;test-2.txt&quot;</span>);

  <span class="hljs-comment">// &#x83B7;&#x53D6;&#x6587;&#x4EF6; size</span>
  <span class="hljs-built_in">print</span>(<span class="hljs-keyword">await</span> file.length());
}
</code></pre>
<p>&#x76F8;&#x5E94;&#x7684;&#xFF0C;&#x8FD9;&#x4E9B;&#x65B9;&#x6CD5;&#x8FD8;&#x6709;&#x4E00;&#x4E2A;&#x5E26;<code>Sync</code>&#x540E;&#x7F00;&#x7684;&#x540C;&#x6B65;&#x7248;&#x672C;&#x65B9;&#x6CD5;&#xFF0C;&#x4F8B;&#x5982;<code>copySync</code>&#x3001;<code>renameSync</code>&#x7B49;&#x3002;</p>
<p>&#x8981;&#x83B7;&#x53D6;&#x6587;&#x4EF6;&#x66F4;&#x591A;&#x7684;&#x4FE1;&#x606F;&#xFF0C;&#x8FD8;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;<code>File</code>&#x7B49;&#x591A;&#x4E2A;&#x7C7B;&#x7684;&#x8D85;&#x7C7B;<code>FileSystemEntity</code>&#x6765;&#x64CD;&#x4F5C;</p>
<pre><code class="lang-dart"><span class="hljs-keyword">import</span> <span class="hljs-string">&apos;dart:io&apos;</span>;

<span class="hljs-keyword">void</span> main() <span class="hljs-keyword">async</span>{
  <span class="hljs-built_in">String</span> path = <span class="hljs-string">&apos;test.txt&apos;</span>;

  <span class="hljs-comment">// &#x5224;&#x65AD;&#x8DEF;&#x5F84;&#x662F;&#x5426;&#x662F;&#x6587;&#x4EF6;&#x5939;</span>
  <span class="hljs-keyword">if</span> (!<span class="hljs-keyword">await</span> FileSystemEntity.isDirectory(path)) {
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&apos;$path is not a directory&apos;</span>);
  } 

 Directory dir = Directory(<span class="hljs-string">r&apos;D:\workspace\dart_space\Tutorial&apos;</span>);
 <span class="hljs-comment">// &#x76EE;&#x5F55;&#x662F;&#x5426;&#x5B58;&#x5728;</span>
 <span class="hljs-keyword">if</span>(<span class="hljs-keyword">await</span> dir.exists()){
   <span class="hljs-comment">// &#x4ECE;&#x76EE;&#x5F55;&#x7684;list&#x65B9;&#x6CD5;&#x83B7;&#x53D6;FileSystemEntity&#x5BF9;&#x8C61;</span>
   Stream&lt;FileSystemEntity&gt; fse = <span class="hljs-keyword">await</span> dir.list();
   <span class="hljs-keyword">await</span> <span class="hljs-keyword">for</span> (FileSystemEntity entity <span class="hljs-keyword">in</span> fse) {
     <span class="hljs-keyword">if</span>(entity <span class="hljs-keyword">is</span> File){
       <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;entity is file&quot;</span>);
     }

     <span class="hljs-comment">// &#x6253;&#x5370;&#x6587;&#x4EF6;&#x4FE1;&#x606F;</span>
     <span class="hljs-built_in">print</span>(<span class="hljs-keyword">await</span> entity.stat());
     <span class="hljs-comment">// &#x5220;&#x9664;</span>
     <span class="hljs-keyword">await</span> entity.delete();
   }
 }<span class="hljs-keyword">else</span>{
   <span class="hljs-comment">// &#x4E0D;&#x5B58;&#x5728;&#x5219;&#x521B;&#x5EFA;&#x3002;recursive&#x4E3A;true&#x65F6;&#xFF0C;&#x521B;&#x5EFA;&#x8DEF;&#x5F84;&#x4E0A;&#x6240;&#x6709;&#x4E0D;&#x5B58;&#x5728;&#x7684;&#x76EE;&#x5F55;</span>
   <span class="hljs-keyword">await</span> dir.create(recursive: <span class="hljs-keyword">true</span>);
 }
}
</code></pre>
<p>&#x9700;&#x6CE8;&#x610F;&#xFF0C;<code>delete</code>&#x4E2D;&#x5305;&#x542B;&#x4E00;&#x4E2A;&#x53EF;&#x9009;&#x7684;&#x53C2;&#x6570;&#xFF0C;&#x539F;&#x578B;<code>Future&lt;FileSystemEntity&gt; delete({bool recursive: false})</code>&#xFF0C;<code>recursive</code>&#x9ED8;&#x8BA4;&#x4E3A;false&#xFF0C;&#x5F53;&#x5220;&#x9664;&#x76EE;&#x5F55;&#x65F6;&#xFF0C;&#x76EE;&#x5F55;&#x5FC5;&#x987B;&#x4E3A;&#x7A7A;&#x624D;&#x80FD;&#x5220;&#x9664;&#xFF1B;&#x5F53;<code>recursive</code>&#x8BBE;&#x7F6E;&#x4E3A;true&#x65F6;&#xFF0C;&#x5C06;&#x5220;&#x9664;&#x76EE;&#x5F55;&#x4E0B;&#x7684;&#x6240;&#x6709;&#x5B50;&#x76EE;&#x5F55;&#x53CA;&#x6587;&#x4EF6;&#x3002;</p>
<hr>
<p><strong>&#x516C;&#x4F17;&#x53F7;&#x201C;&#x7F16;&#x7A0B;&#x4E4B;&#x8DEF;&#x4ECE;0&#x5230;1&#x201D;</strong></p>
<p><img src="https://gitee.com/arcticfox1919/ImageHosting/raw/master/img/20190301102949549.jpg" alt="20190301102949549"> </p>
<footer class="page-footer"><span class="copyright">Copyright &#xA9; Arcticfox 2020 all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">&#x6587;&#x6863;&#x4FEE;&#x8BA2;&#x4E8E;&#xFF1A;
2021-03-18 13:32:58
</span></footer>
<script>console.log("plugin-popup....");document.onclick = function(e){ e.target.tagName === "IMG" && window.open(e.target.src,e.target.src)}</script><style>img{cursor:pointer}</style>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="生成器.html" class="navigation navigation-prev " aria-label="Previous page: 生成器">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="运算符.html" class="navigation navigation-next " aria-label="Next page: 附：运算符">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"标准流与文件","level":"1.3.8","depth":2,"next":{"title":"附：运算符","level":"1.3.9","depth":2,"path":"运算符.md","ref":"运算符.md","articles":[]},"previous":{"title":"生成器","level":"1.3.7","depth":2,"path":"生成器.md","ref":"生成器.md","articles":[]},"dir":"ltr"},"config":{"plugins":["anchor-navigation-ex","expandable-chapters-small","-lunr","-search","search-pro","-sharing","github","expandable-chapters","pageview-count","theme-comscore","tbfed-pagefooter","popup","custom-favicon"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"Copyright &copy Arcticfox 2020","modify_label":"文档修订于：","modify_format":"YYYY-MM-DD HH:mm:ss"},"github":{"url":"https://github.com/arcticfox1919"},"search-pro":{},"popup":{},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"anchor-navigation-ex":{"associatedWithSummary":true,"float":{"floatIcon":"fa fa-navicon","level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"mode":"float","multipleH1":true,"pageTop":{"level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"printLog":false,"showGoTop":true,"showLevel":true},"favicon":"logo.ico","theme-comscore":{},"pageview-count":{},"custom-favicon":{},"expandable-chapters-small":{},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"expandable-chapters":{}},"theme":"default","author":"arcticfox","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"Dart编程指南","language":"zh-hans","links":{"sidebar":{"主页":"http://www.bczl.xyz/"}},"gitbook":"*"},"file":{"path":"标准流与文件.md","mtime":"2021-03-18T05:32:58.025Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2021-10-18T14:58:03.414Z"},"basePath":".","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="gitbook/gitbook.js"></script>
    <script src="gitbook/theme.js"></script>
    
        
        <script src="gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search-pro/jquery.mark.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search-pro/search.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-github/plugin.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-pageview-count/plugin.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-theme-comscore/test.js"></script>
        
    

    </body>
</html>

